package Concurrency.AsynTaskExecute;

/**
 * 演示Timer死循环示例
 * 一个Timer对象中只有一个线程
 */

import java.util.Timer;
import java.util.TimerTask;

/**
 * Timer死循环实例
 */

public class EndlessLoopTimer {
    static class LoopTask extends TimerTask {
        @Override
        public void run() {
            while (true) {
                try {
                    //模拟执行任务
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class ExampleTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("hello");
        }
    }

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new LoopTask(), 10);
        timer.schedule(new ExampleTask(), 100);
    }
}
